<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/27
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Owner;
use think\Db;
use think\Config;

/**
 * 普通订单🗂
 */
class Order1 extends Owner
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['money_change_order1_deposit'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $db = 'order1';


    // /**
    //  * 搜索
    //  *
    //  * @param string $str 搜索内容
    //  * @return void
    //  */
    // public function test($str = 'xiao')
    // {
    //     $where['username|nickname'] = ['LIKE','%'.$str.'%'];
    //     $list = Db::name('user')
    //         ->where($where)
    //         ->select();
    //     $this->success('ok',$list);
    // }


     /**
     * 发货人下单📦
     *
     * @param int $s_addr_id 装货地址
     * @param int $s2_addr_id 装货地址2
     * @param int $e_addr_id 卸货地址
     * @param int $e2_addr_id 卸货地址2  
     * @param int $s_load_time 装货开始时间
     * @param int $s_load_str 装货时间文字
     * @param int $e_load_time 装货结束时间            
     * @param string $money_status  费用方案:1=平台估价,2=货主出价,3=电话议价
     * @param string $s_ps 发货人备注信息            
     * @param string $load_status 装车类型:1=整车,2=零担
     * @param int $car2_id 车2id（整车必须传）
     * @param decimal $weight 吨(零必传)担
     * @param string $goods_name 货物名称
     * @param decimal $long 长
     * @param decimal $wide 宽
     * @param decimal $high 高
     * @param string $tag_ids 标签ids
     * @param string $often_status 常发货源:0=否,1=是
     * @param decimal $km 预计公里数(km)
     * @param decimal $money 估计价格(必传)
     * @param decimal $s_assess_money 发货人出价
     * @param int $deposit_status 默认1押金退款:1=退还给司机,2=退还给发货人
     * @return void
     */
    public function add_order(  $s_addr_id = null,
            $s2_addr_id = null,
            $e_addr_id = null,
            $e2_addr_id = null,
            $s_load_time = null,
            $s_load_str = null,
            
            $e_load_time = null,
            $money_status = null,
            $s_ps = null,
            $load_status = null,
            $goods_name = null,
            $long = null,
            $wide = null,
            $high = null,$weight = null,$tag_ids=null,$often_status=0,$km=null,$money=null,$car2_id=null,$s_assess_money=null,$deposit_status='1'){
 
        // $params = $this->request->param();
        //     $fields = [   ];


        $data['s_addr_id'] = $s_addr_id;
        $data['s2_addr_id'] = $s2_addr_id;
        $data['e_addr_id'] = $e_addr_id;
        $data['e2_addr_id'] = $e2_addr_id;
        $data['s_load_str'] = $s_load_str;
        $data['s_load_time'] = strtotime($s_load_time);
        $data['e_load_time'] = strtotime($e_load_time);
        $data['money_status'] = $money_status;
        $data['s_ps'] = $s_ps;
        $data['load_status'] = $load_status;
        $data['goods_name'] = $goods_name;
        $data['long'] = $long;
        $data['wide'] = $wide;
        $data['high'] = $high;
        $data['weight'] = $weight;
        $data['tag_ids'] = $tag_ids;
        $data['often_status'] = $often_status;
        $data['car2_id'] = $car2_id;
        $data['km'] = $km;

        $data['money'] = $money;

       
        $data['deposit_status'] = $deposit_status;
        if($s_assess_money>0){
            $data['s_assess_money'] = $s_assess_money;//货主出价
            $data['s_money'] = $this->ChangeOrder1Deposit($s_assess_money);//Config::get('site.s_money1');
        }else{
            $data['s_money'] = $this->ChangeOrder1Deposit($money);//Config::get('site.s_money1');
        }
        
        $data['j_money'] = $data['s_money'];//Config::get('site.j_money1');

        //     $data = $this->get_field_arr($params,$fields,true);
            $data['s_uid'] = $this->auth->id;
            
            $data['status'] = '1';//'状态:1=待接单,2=已接单,3=已完成,9=已取消',
            $data['lay_status'] = '1';//装卸类型:1=一装一卸,2=一装两卸,3=两装一卸,4=两装两卸',

            
            $this->AddAddrUseNum($data['s_addr_id']);
            $this->AddAddrUseNum($data['e_addr_id']);
            if(!empty($data['s2_addr_id'])){
                $this->AddAddrUseNum($data['s2_addr_id']);
                if(!empty($data['e2_addr_id'])){
                    $data['lay_status'] = '4';
                    $this->AddAddrUseNum($data['e2_addr_id']);
                }else{
                    $data['lay_status'] = '3';
                }
            }elseif(!empty($data['e2_addr_id'])){
                $data['lay_status'] = '2';
                $this->AddAddrUseNum($data['e2_addr_id']);
            }
            $data['order'] = $this->rand_order('Order1',$this->auth->id);

            switch ($data['money_status']) {//费用方案:1=平台估价,2=货主出价,3=电话议价
                case '1':
                  
                    //装车类型:1=整车,2=零担
                    switch ($data['load_status']) {
                        case '1':
                            // $this->Long_Wide_High_GetBulkloadPrice($order,$long ,$wide ,$high ,$km  ,$t )
                            break;
                        case '2':
                            // $this->Long_Wide_High_GetBulkloadPrice($order,$long ,$wide ,$high ,$km  ,$t )
                            break;
                        
                        default:
                            # code...
                            break;
                    }
                    break;
                
                default:
                    # code...
                    break;
            }
            $data['size'] = $long * $wide * $weight;

            $data['time'] = time();
            $data['id'] = Db::name($this->db)->insertGetId($data);

            $this->success('请选择支付方式',$data);

            //支付订单
            $pay_data = $this->PayOrder($data['id'],1,$this->auth->id,'1');
            $this->success('pay',$pay_data);
        //     `j_time` int(10) DEFAULT NULL COMMENT '接单时间',
        //    
        //     `order` varchar(50) DEFAULT NULL COMMENT '订单号',
        //     `money` varchar(255) DEFAULT NULL COMMENT '估价',
        //     `s_uid` int(10) DEFAULT NULL COMMENT '发货人',
        //     `j_uid` int(10) DEFAULT NULL COMMENT '接单人',
        //     `s_money` decimal(8,2) DEFAULT NULL COMMENT '发货人押金',
        //     `j_money` decimal(8,2) DEFAULT NULL COMMENT '接单人押金',
        //     `time` int(10) unsigned DEFAULT NULL COMMENT '添加时间',
        //     `spay_status` enum('1','2','3') DEFAULT '2' COMMENT '发货人支付押金:1=已支付,2=未支付,3=已违约',
        //     `jpay_status` enum('1','2','3') DEFAULT '2' COMMENT '接单人支付押金:1=已支付,2=未支付,3=已违约',
        //     PRIMARY KEY (`id`)
        //   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='普通发货订单';
    }


    /**
     * 发货人支付未支付的订单💰
     *
     * @param string $order 订单号
     * @param string $pay_type 支付类型:1=微信,2=支付宝,3=钱包,9=其他默认3
     * @return void
     */
    public function s_pay_order($order,$pay_type='3')
    {
        $data = Db::name($this->db)->where('order',$order)->find();
        if(!$data){
            $this->error('没有此订单');
        }elseif($data['spay_status']!='2'){//发货人支付押金:1=已支付,2=未支付,3=已违约
            $this->error('已支付');
        }

        //     * @param string $order_id 订单id
        //  * @param string $status 订单类型
        //  * @param string $uid 用户id
        //  * @param string $role_status 角色:1=货主,2=司机
        //  * @param string $pay_type 支付类型:1=微信,2=支付宝,9=其他
        $res = $this->PayOrder($data['id'],1,$this->auth->id,'1',$pay_type);
        $this->success('pay',$res);
    }



     /**
      * 获取发货列表
      *
      * @param integer $page 1
      * @param integer $limit 5
      * @param integer $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
      * @return void
      */
    public function get_order_list($page=1,$limit=5,$status=null)
    {
        $where['s_uid'] = ['=',$this->auth->id];
        if($status){
            $where['status'] = ['=',$status];
        }
        $list = Db::name($this->db)
            ->where($where)
            ->page($page,$limit)
            ->order('time desc')
            ->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder1FieldData($val);                 
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // if($val['s2_addr_id']>0)
                //     $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                // if($val['e2_addr_id']>0)
                //     $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
                $val['user'] = $this->GetJuserData($val['j_uid']);
            }
        }
        $this->success('ok',$list);
    }




    /**
     * 获取待完成的订单列表
     *
     * @param integer $page 1
     * @param integer $limit 5
     * @return void
     */
    public function get_todo_order_list($page=1,$limit=5)
    {
       
        $where['s_uid'] = ['=',$this->auth->id];
    
        $where['status'] = ['<',6];//$status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
        $where['spay_status'] = '1';//发货人支付押金:1=已支付,2=未支付,3=已违约
        
        $list = Db::name($this->db)
            ->where($where)
            ->page($page,$limit)
            ->order('time desc')
            ->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder1FieldData($val);                 
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // if($val['s2_addr_id']>0)
                //     $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                // if($val['e2_addr_id']>0)
                //     $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
                $val['user'] = $this->GetJuserData($val['j_uid']);
            }
        }
        $this->success('ok',$list);
    }

    /**
     * 获取常发货列表
     *
     * @return void
     */
    public function get_often_list()
    {
        $list = Db::name($this->db)->where('often_status','1')->where('s_uid',$this->auth->id)->order('time desc')->select();
        if($list){
            foreach ($list as $key => &$val) {
                $val = $this->GetOrder1FieldData($val);                 
                // $val['s_addr'] = $this->GetAddrData($val['s_addr_id']);
                // $val['e_addr'] = $this->GetAddrData($val['e_addr_id']);
                // if($val['s2_addr_id']>0)
                //     $val['s2_addr'] = $this->GetAddrData($val['s2_addr_id']);
                // if($val['e2_addr_id']>0)
                //     $val['e2_addr'] = $this->GetAddrData($val['e2_addr_id']);
                $val['user'] = $this->GetJuserData($val['j_uid']);
            }
        }
        $this->success('ok',$list);
    }

    /**
     * 获取常发货信息
     *
     * @param int $id 常发货源id
     * @return void
     */
    public function get_often_data($id)
    {
        $val = Db::name($this->db)->where('often_status','1')->where('s_uid',$this->auth->id)->where('id',$id)->find();

        $val = $this->GetOrder1FieldData($val); 
        $val['user'] = $this->GetJuserData($val['j_uid']);
        $this->success('ok',$val);
    }

    /**
     * 获取普通发货信息
     * 
     * @param int $id 订单id
     * @return void
     */ 
    public function get_order1_data($id)
    {
        $val = $this->GetOrder1Data($id);
        $val['user'] = $this->GetJuserData($val['j_uid']);
        $this->success('ok',$val);
    }


    /**
     * 用户更新订单1状态
     *
     * @param int $id 订单号id
     * @param int $status 状态:1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
     * @return void
     */
    public function update_order1_status($id,$status)
    {
        $where['s_uid'] = $this->auth->id;
        $where['id'] = $id;
        $res = Db::name($this->db)
                ->where($where)
                ->find();
        if(!$res){
            $this->error('没有该订单');
        }
        switch ($status) {
            case '1':
                // if($res['status']!='9')
                // $this->error('当前订单待接单');
                break;

            case '2':
                // if($res['status']!='9')
                $this->error('当前已接单,不能修改装');
                break;

            case '3':
                if($res['status']!='2')
                $this->error('未接单');
                
                break;

            case '4':         
                if($res['status']!='3')      
                $this->error('司机未到达目的地');
                
                break;

            case '5':
                if($res['status']!='4')
                $this->error('货主未确认已装车');
                
                break;

            case '6':     
                if($res['status']!='5')
                $this->error('司机未确认送达');
                break;
        //1=待接单,2=已接单,3=司机到达目的地,4=货主确认已装车,5=司机已确认送达,6=货主已确认收货,9=已取消
            default:
                $this->error('暂未加入此状态');
                break;
        }
        $this->UpdateOrder1Status($id,$status);

        $this->success('修改成功');
    }

    /**
     * 修改订单状态
     *
     * @param int $id 订单id
     * @param int $status 状态
     * @return void
     */
    protected function UpdateOrder1Status($id,$status){

        //后期加入ws推送给司机
        Db::name($this->db)->where('id',$id)->update(['status'=>$status]);
    }



}
